import api from "../../http/apis/apis"
import { Message } from 'element-ui'
export default {
    namespaced: true,
    state: {
        carData: {
            data: [],
            total: 0,
            currentPage: 1,
            pageSize: 8,
            totalPages: 0
        },
        upData: []
    },
    mutations: {
        setCarData(state, newCarData) {
            state.carData = newCarData;
        },
        setCurrent(state, newCurrent) {
            state.carData.currentPage = newCurrent
        },
        setPageSize(state, newPagesize) {
            state.carData.pageSize = newPagesize
        },
        setUpData(state, newUpData) {
            state.upData = newUpData
        }
    },
    actions: {
        async addCar(context, addCarData) {
            console.log(addCarData);
            const data = await api.carApi.addCar(addCarData)
            return data
        },

        async findAllCar(context, type) {
            const data = await api.carApi.findAllCar({
                currentPage: context.state.carData.currentPage,
                pageSize: context.state.carData.pageSize,
            })
            console.log(data.data.data);
            if (data.status) {
                context.commit('setCarData', data.data.data)
            }
            // console.log('car',data.data.data);
            return data
        },

        async delCar(context, _id) {
            const { data } = await api.carApi.delCar({
                _id: _id,
            })
            if (data.status) {
                context.dispatch("findAllCar")
            }
        },
 
        async clickChangeStatus(context, changeData) {
            console.log(changeData);
            const { _id, type } = changeData
            const { data } = await api.carApi.clickChangeStatus({ _id, type })
            if (data.status) {
                context.dispatch("findAllCar")
                Message.success(data.msg);
            }
            return data
        },
        async clickChangeVerify(context, changeData) {
            console.log(changeData);
            const { data } = await api.carApi.clickChangeVerify(changeData)
            if (data.status) {
                context.dispatch("findAllCar")
                Message.success(data.msg);
            }
            return data
        },
        async findCarByID(context, _id) {
            console.log(_id, 123);
            const { data } = await api.carApi.findCarByID({ _id })
            if (data.status) {
                context.commit('setUpData', data.data)
            }
            return data.data
        },
        async changeCar(context, changeFormData) {
            const { data } = await api.carApi.changeCar(changeFormData)
            if (data.status) {
                context.dispatch("findAllCar")
                Message.success(data.msg);
            }
        }
    },
    model: {
    }
}